package com.bz.sunmipay.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.bz.sunmipay.R;
import com.bz.sunmipay.activity.LoginActivity;
import com.bz.sunmipay.activity.MainActivity;
import com.bz.sunmipay.activity.PaymentChargeActivity;
import com.bz.sunmipay.activity.PaymentFailActivity;
import com.bz.sunmipay.activity.PaymentMethodActivity;
import com.bz.sunmipay.activity.PaymentPatternActivity;
import com.bz.sunmipay.activity.PaymentSuccessActivity;
import com.bz.sunmipay.activity.PerformanceActivity;
import com.bz.sunmipay.model.HomeInfo;

import java.io.File;
import java.io.IOException;

/**
 * #{DESC}
 * <p>
 * Created by liuyakui on 2019-09-07.
 */
public class IntentUtils {
    private static final String SCHEME = "sunmi://";

    //交易记录页
    public static final String PERFORMANCE_LINK = SCHEME + "performance";

    public static final int PAGE_FROM_CASH = 1;
//    public static final int PAGE_FROM_CARD = 2;

    public static void goToHome(Context context) {
        final Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    public static void goToHomeItemPager(Context context, HomeInfo homeInfo) {
        final Intent intent = initIntent(homeInfo.getIntentUrl());
        if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
        }
    }

    public static void goToHomeItemPager(Context context, String link) {
        final Intent intent = initIntent(link);
        if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
        }
    }

    private static Intent initIntent(final String uriString) {
        final Intent intent = new Intent();
        intent.setData(Uri.parse(uriString));
        return intent;
    }

    public static void goToLoginPage(Context context) {
        final Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    public static void goToPaymentSuccessPage(Context context) {
        final Intent intent = new Intent(context, PaymentSuccessActivity.class);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    //支付方式页面
    public static void goToPaymentPatternPage(Context context, String amount, String vat) {
        final Intent intent = new Intent(context, PaymentPatternActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("vat", vat);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    //支付明细页
    public static void goToPaymentPerformancePage(Context context) {
        final Intent intent = new Intent(context, PerformanceActivity.class);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    public static void goToPaymentFailPage(Context context) {
        final Intent intent = new Intent(context, PaymentFailActivity.class);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    public static void goToPaymentMethod(Context context) {
        final Intent intent = new Intent(context, PaymentMethodActivity.class);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    public static void goToPaymentCharge(Context context, String amount, String vat, int pageFrom) {
        final Intent intent = new Intent(context, PaymentChargeActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("vat", vat);
        intent.putExtra("pageFrom", pageFrom);
        ((Activity) context).startActivityForResult(intent, 1001);
        ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.scale_hold);
    }

    public static void goToInstallApk(Context context, Uri apk) {
        try {
            chmod("777", apk.getPath());
            final Intent intent = new Intent(Intent.ACTION_VIEW);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(getUriForFile(context, apk.getPath()), "application/vnd.android.package-archive");
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Uri getUriForFile(Context context, String path) {
        File file = new File(path);
        if (context == null) {
            return null;
        }
        Uri uri = null;
        try {
            //Android7.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".fileprovider", file);
            } else {
                uri = Uri.fromFile(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uri;
    }

    /**
     * 获取权限
     *
     * @param permission 权限
     * @param path       路径
     */
    public static void chmod(String permission, String path) {
        try {
            String command = "chmod " + permission + " " + path;
            Runtime runtime = Runtime.getRuntime();
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
